package com.lanou.bookstore.book.web.servlet.admin;

import com.lanou.bookstore.book.dao.BookDao;
import com.lanou.bookstore.book.dao.BookDaoImpl;
import com.lanou.bookstore.book.domain.Book;
import com.lanou.bookstore.book.service.BookService;
import com.lanou.bookstore.book.service.BookServiceImpl;
import com.lanou.bookstore.category.dao.CategoryDao;
import com.lanou.bookstore.category.dao.CategoryDaoImpl;
import com.lanou.bookstore.category.domain.Category;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/abs")
public class AdminBookServlet extends HttpServlet {

    BookService bs = new BookServiceImpl();
    CategoryDao cateDao = new CategoryDaoImpl();
    BookDao bookDao = new BookDaoImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String para = req.getParameter("para");
        String bid = req.getParameter("bid");
        String method = req.getParameter("method");


        String bname = req.getParameter("bname");
        String price = req.getParameter("price");
        String author = req.getParameter("author");
        String cid = req.getParameter("cid");


        if ("toadd".equals(para)) {
            List<Category> categoryList = cateDao.queryAll();
            req.getSession().setAttribute("categoryList", categoryList);
            req.getRequestDispatcher("/adminjsps/admin/book/add.jsp").forward(req, resp);

        } else if ("tomod".equals(para)) {
            List<Category> categoryList = cateDao.queryAll();
            req.setAttribute("categoryList", categoryList);
            List<Book> list = bookDao.queryByDid(bid);
            Book book = new Book();
            for (Book book1 : list) {
                book = book1;
            }
            req.getSession().setAttribute("book", book);
            req.getRequestDispatcher("/adminjsps/admin/book/desc.jsp").forward(req, resp);



        } else if ("add".equals(para)) {

//            String bname = req.getParameter("bname");
            //todo 文件上传暂时搁置
//            String image = req.getParameter("image");
//            String price = req.getParameter("price");
//            String author = req.getParameter("author");
//            String cid = req.getParameter("cid");
            Book book = new Book();
            book.setBname(bname);
//            book.setImage(image);
            book.setPrice(Double.parseDouble(price));
            book.setAuthor(author);
            book.setCid(cid);


            try {

                bs.addBook(book);
            } catch (Exception e) {
                e.printStackTrace();

            }


        } else if ("query".equals(para)) {
            List<Book> list = bs.queryAll();
            req.getSession().setAttribute("adminBookList", list);
            req.getRequestDispatcher("/adminjsps/admin/book/list.jsp").forward(req, resp);
        }else if ("del".equals(method)){

            bookDao.delBook(bid);
            req.getRequestDispatcher("/abs?para=query").forward(req, resp);


        }else if ("mod".equals(method)){
            Book book = new Book();
            book.setBname(bname);
            book.setAuthor(author);
            book.setPrice(Double.parseDouble(price));
            List<Category> categoryList = cateDao.queryAll();
            for (Category category : categoryList) {
                if (category.getCname().equals(cid)){
                    book.setCid(category.getCid());
                }
            }



            bookDao.modBook(book,bid);

            req.getRequestDispatcher("/abs?para=query").forward(req, resp);


        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        doGet(req, resp);
    }
}
